/**
 * 实现堆栈
 */

#include <stdio.h>
#include "calc.h"

#define MAXVAL 100

static int sp = 0;
static double val[MAXVAL];

void push(double f) {
    if (sp >= MAXVAL)
        printf("error: stack full, can't push %g\n", f);
    else
        val[sp++] = f;
}

double pop(void) {
    if (sp > 0)
        return val[--sp];
    else {
        printf("error: stack empty\n");
        return 0.0;
    }
}

void swapItems() {
    double op1 = pop();
    double op2 = pop();
    push(op1);
    push(op2);
}

void duplicate() {
    double temp = pop();
    push(temp);
    push(temp);
}

void clear() {
    sp = 0;
}

void showTop() {
    if (sp > 0)
        printf("top of the stack is %g\n", val[sp-1]);
    else
        printf("stack is empty\n");
}
